package com.gxa.hualian.web.listenter;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebListener
public class MyListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {

    public MyListener() {
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        /* This method is called when the servlet context is initialized(when the Web application is deployed). */
        /*
        当上下文对象被初始化的时候执行的操作
         */
        System.out.println("contextInitialized当上下文对象被初始化的时候执行的操作");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        /* This method is called when the servlet Context is undeployed or Application Server shuts down. */
    /*
        当上下文对象被销毁的时候执行的操作
     */
        System.out.println("contextDestroyed  当上下文对象被销毁的时候执行的操作");
    }
    //表示在线人数
    int count=0;
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
        System.out.println("sessionCreated当session被创建的时候执行");

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
        System.out.println("sessionDestroyed当session被销毁的时候执行");

    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent sbe) {
        /* This method is called when an attribute is added to a session. */
        System.out.println("attributeAdded当session中的数据被添加的时候执行");
        count++;
        System.out.println("系统当前的在线人数:"+count);
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent sbe) {
        /* This method is called when an attribute is removed from a session. */
        System.out.println("attributeRemoved当session中的数据被移除的时候执行");
        count--;
        System.out.println("系统当前的在线人数:"+count);
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent sbe) {
        /* This method is called when an attribute is replaced in a session. */
        System.out.println("attributeReplaced当session中的数据被替换的时候执行");
    }
}
